<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Exception;
use think\Log;


class System extends AdminBase
{
    private $star_level_config_model;
    private $system_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->star_level_config_model = model('StarLevelConfig');
        $this->system_model = model('System');

    }

    /**
     * 获取客户的星级评定配置
     * 一星级	偶然性客户
     * 二星级	业务额≤5000
     * 三星级	5000＜业务额≤20000
     * 四星级	20000＜业务额≤50000
     * 五星级	50000＜业务额
     */
    public function star_level_config_index()
    {
        $config_info = $this->star_level_config_model->getList([]);

        return $this->fetch('sl_config', [
            'config_info' => $config_info,
        ]);
    }

    /**
     * 修改星级评定标准配置
     */
    public function star_level_config()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id');
            if ($id <= 0) {
                return ajax_return_error('缺少id参数！');
            }
            $revenue_min = $this->request->param('revenue_min', '', 'trim');
            $revenue_max = $this->request->param('revenue_max', '', 'trim');

            if (empty($revenue_min)) {
                $revenue_min = 0;
            } else {
                if (!preg_match('/^[0-9]+(\.[0-9]+)?$/', $revenue_min)) {
                    return ajax_return_error('请填写正确的数据');
                }
            }
            if (empty($revenue_max)) {
                $revenue_max = 0;
            } else {
                if (!preg_match('/^[0-9]+(\.[0-9]+)?$/', $revenue_max)) {
                    return ajax_return_error('请填写正确的数据');
                }
            }

            $desc = $this->request->param('desc', '', 'app\common\controller\ParamFilter::normalParam');
            if ($revenue_min <= 0 && $revenue_max <= 0 && empty($desc)) {
                return ajax_return_error('请填写配置内容');
            }

            $data = [
                'revenue_min' => $revenue_min,
                'revenue_max' => $revenue_max,
                'desc' => $desc,
            ];
            $res = $this->star_level_config_model->saveDate($id, $data);
            if ($res === false) {
                return ajax_return_error('修改失败，请稍后重试！');
            } else {
                return ajax_return_adv('修改成功！');
            }
        }
    }

    /**
     * 网站的一些其他配置项
     */
    public function other_set()
    {
        $config_logic = logic('Config');
        $set_list = $config_logic->getOtherSetList();
        $flag = 0;
        $alert_msg = '';
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!empty($data)) {
                try {
                    $set_list_names = array_column($set_list, 'name');
                    foreach ($data as $k => &$v) {
                        if (in_array($k, $set_list_names)) {
                            if (empty($v)) {
                                exception('配置内容不能为空！');
                            }

                            if ($k == CUSTOMER_NUMBER_PREFIX_KEY && !preg_match('/^[a-zA-Z]{1,4}$/', $v)) {
                                exception('前缀只能输入：1-4位字母！');
                            }
                            $v = strtoupper($v);

                            $res = $config_logic->saveOtherConfig($k, $v);
                            if (!$res) {
                                exception('修改失败！');
                            } else {
                                $set_list = $config_logic->getOtherSetList();
                                $flag = 1;
                                $alert_msg = '修改成功！';
                            }
                        } else {
                            exception('异常请求！');
                        }
                        break;
                    }
                } catch (Exception $e) {
                    $flag = 2;
                    $alert_msg = $e->getMessage();
                }
            }
        }

        return $this->fetch('other_set', [
            'set_list' => $set_list,
            'flag' => $flag,
            'alert_msg' => $alert_msg,
        ]);
    }
}
